use crate::{SM4, Cipher};

#[test]
fn sm4() {
    // (text, key, ciphertxt, nums)
    let cases = [
        (
            [0x01u8, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef, 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10],
            [0x01u8, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef, 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10],
            [0x68u8, 0x1e, 0xdf, 0x34, 0xd2, 0x06, 0x96, 0x5e, 0x86, 0xb3, 0xe9, 0x4f, 0x53, 0x6e, 0x42, 0x46],
            1usize,
        ),
        (
            [0x01u8, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef, 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10],
            [0x01u8, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef, 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10],
            [0x59, 0x52, 0x98, 0xc7, 0xc6, 0xfd, 0x27, 0x1f, 0x04, 0x02, 0xf8, 0x04, 0xc3, 0x3d, 0x3f, 0x66],
            1_000_000usize,
        ),
    ];
    
    cases.iter().enumerate().for_each(|(i, ele)| {
        let sm4 = SM4::new(ele.1);
        let (mut buf0, mut buf1) = (Vec::with_capacity(16), Vec::with_capacity(16));
        let (mut buf0, mut buf1) = (&mut buf0, &mut buf1);
        ele.1.iter().for_each(|&e| {buf1.push(e);});
        for _ in 0..(ele.3) {
            sm4.encrypt(buf0, buf1.as_slice()).unwrap();
            let tmp = buf0;
            buf0 = buf1;
            buf1 = tmp;
        }
        
        assert_eq!(buf1.as_slice(), ele.2.as_ref(), "encrypt-case:{}", i);
        
        buf1.clear();
        ele.2.iter().for_each(|&e| {buf1.push(e);});
        for _ in 0..(ele.3) {
            sm4.decrypt(buf0, buf1.as_slice()).unwrap();
            let tmp = buf0;
            buf0 = buf1;
            buf1 = tmp;
        }
        assert_eq!(buf1.as_slice(), ele.0.as_ref(), "encrypt-case:{}", i);
    });
}